{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵乘法用到了二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创造一个手动的二维矩阵 \n",
    "def creata_n_dimension_matrix(n):\n",
    "    # 初始化为0\n",
    "    return [[0] * n for _ in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def matrix_multiply(a, b, n):\n",
    "    # 初始化一个 n x n 的矩阵 b，存储乘积结果\n",
    "    result = [[0] * n for _ in range(n)]\n",
    "    \n",
    "    # 进行矩阵相乘\n",
    "    for i in range(n):\n",
    "        for j in range(n):\n",
    "            for k in range(n):\n",
    "                result[i][j] += a[i][k] * b[k][j]\n",
    "    \n",
    "    return result\n",
    "\n",
    "def matrix_power(n, m, a):\n",
    "    # 初始化矩阵 t 为 a\n",
    "    t = [row[:] for row in a]  # t = a 的副本\n",
    "\n",
    "    # 当 m 为 0 时，输出单位矩阵\n",
    "    if m == 0:\n",
    "        return [[1 if i == j else 0 for j in range(n)] for i in range(n)]\n",
    "    \n",
    "    # 进行 m-1 次矩阵乘法\n",
    "    for _ in range(m - 1):\n",
    "        t = matrix_multiply(t, a, n)  # 矩阵乘法\n",
    "\n",
    "    return t\n",
    "\n",
    "# 输入矩阵的大小和 m 值\n",
    "n, m = map(int, input().split())\n",
    "\n",
    "# 输入矩阵 a\n",
    "a = [list(map(int, input().split())) for _ in range(n)]\n",
    "\n",
    "# 计算并输出结果矩阵\n",
    "result = matrix_power(n, m, a)\n",
    "\n",
    "# 输出最终矩阵\n",
    "for row in result:\n",
    "    print(\" \".join(map(str, row)))\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
